from pydantic import BaseModel
from typing import Optional, List
from datetime import datetime

class ModuleBase(BaseModel):
    name: str
    code: str
    description: Optional[str] = None
    icon: Optional[str] = None
    route: Optional[str] = None
    parent_id: Optional[int] = None
    order: Optional[int] = 0
    is_active: Optional[bool] = True

class ModuleCreate(ModuleBase):
    pass

class ModuleUpdate(BaseModel):
    name: Optional[str] = None
    code: Optional[str] = None
    description: Optional[str] = None
    icon: Optional[str] = None
    route: Optional[str] = None
    parent_id: Optional[int] = None
    order: Optional[int] = None
    is_active: Optional[bool] = None

class Module(ModuleBase):
    id: int
    created_at: datetime
    updated_at: Optional[datetime] = None
    children: Optional[List['Module']] = None  # 自引用关系

    class Config:
        from_attributes = True

# 解决循环引用问题
Module.update_forward_refs()

class ModuleTree(Module):
    # 用于返回模块树结构
    children: Optional[List['ModuleTree']] = None

ModuleTree.update_forward_refs()

class AdminModulesResponse(BaseModel):
    # 管理员模块响应
    success: bool = True
    data: List[ModuleTree]
    message: str = "获取成功"